package com.dinadi.yidi.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dinadi.yidi.R;
import com.dinadi.yidi.bean.ExhibitionreaRequestEntity;
import com.dinadi.yidi.ui.activity.ExhibitionMatchActivity;
import com.dinadi.yidi.ui.activity.WorksExhibitionActivity;
import com.dinadi.yidi.utils.ImageUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * author: JiaYu.He
 * Date: 2017/3/24
 * Time: 12:53
 */

public class ExhibitionMatchAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>  {

    private Context mContext;
    private List<ExhibitionreaRequestEntity> mDatas = new ArrayList<>();
   // private String type;
    private String showId1,showId2;
//    /*运用典型的观察者模式添加点击事件*/
//    /*定义接口变量*/
//    private ExhibitionMatchAdapter.OnRecyclerViewItemClickListener mOnItemClickListener = null;


//    public static interface OnRecyclerViewItemClickListener {
//        void onItemClick(View view, String data);
//    }


    public void addAllData(List<ExhibitionreaRequestEntity> dataList) {
        this.mDatas.addAll(dataList);
        notifyDataSetChanged();
    }

    public void clearData() {
        this.mDatas.clear();
    }

    public ExhibitionMatchAdapter(Context context) {
        mContext = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_match_item, parent, false);
        //将创建的View注册点击事件
       // v.setOnClickListener(this);
        return new ExhibitionMatchAdapter.ItemViewHolder(v);


    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {

        ExhibitionMatchAdapter.ItemViewHolder itemViewHolder = (ExhibitionMatchAdapter.ItemViewHolder) holder;
//            String         str            = mDatas.get(position);
        itemViewHolder.text_msg.setText(mDatas.get(position).getTitle());
        String imgUrl = mDatas.get(position).getCoverImg();

        if (imgUrl != null) {
            if (!imgUrl.equals("")) {
                ImageUtils.loadImageView(mContext, imgUrl, itemViewHolder.imageView);
            } else {
                ImageUtils.loadImageView(mContext, R.drawable.symbols_bitmap, itemViewHolder.imageView);
            }
        }else{
            ImageUtils.loadImageView(mContext, R.drawable.symbols_bitmap, itemViewHolder.imageView);

        }

        itemViewHolder.layout_adapter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                String  type = mDatas.get(position).getType();

                if (type.equals("1")) {
                    showId1 = mDatas.get(position).getShowId();
                    intent.setClass(mContext, WorksExhibitionActivity.class);
                    intent.putExtra("showId", showId1);
                } else if (type.equals("2")) {
                    showId2 = mDatas.get(position).getShowId();
                    intent.setClass(mContext, ExhibitionMatchActivity.class);
                    intent.putExtra("showId", showId2);
                }

                mContext.startActivity(intent);
            }
        });

    }


    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    public class ItemViewHolder extends RecyclerView.ViewHolder {

        TextView text_msg;
        ImageView imageView;
        RelativeLayout layout_adapter;

        public ItemViewHolder(View itemView) {
            super(itemView);
            text_msg = (TextView) itemView.findViewById(R.id.text_title);
            imageView = (ImageView) itemView.findViewById(R.id.cover_img);
            layout_adapter= (RelativeLayout) itemView.findViewById(R.id.layout_adapter);
        }


    }


//    /*将事件转移给外面的调用者*/
//    @Override
//    public void onClick(View v) {
//
//        if (mOnItemClickListener != null) {
//            //注意这里使用getTag方法获取数据
//            mOnItemClickListener.onItemClick(v, (String) v.getTag());
//
//        }
//    }
//
//    /*最后暴露给外面的调用者*/
//    public void setmOnItemClickListener(ExhibitionMatchAdapter.OnRecyclerViewItemClickListener listener) {
//        this.mOnItemClickListener = listener;
//
//    }

//    public String getType(){
//        return type;
//    }


}

